<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>textformatserializer_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_proto2_textformatserializer_test.js.html">textformatserializer_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.proto2.TextFormatSerializer
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.proto2.TextFormatSerializerTest&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.proto2.TextFormatSerializer&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line25"></a>goog.require(&#39;proto2.TestAllTypes&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.setTestOnly(&#39;goog.proto2.TextFormatSerializerTest&#39;);
<a name="line28"></a>
<a name="line29"></a>function testSerialization() {
<a name="line30"></a>  var message = new proto2.TestAllTypes();
<a name="line31"></a>
<a name="line32"></a>  // Set the fields.
<a name="line33"></a>  // Singular.
<a name="line34"></a>  message.setOptionalInt32(101);
<a name="line35"></a>  message.setOptionalUint32(103);
<a name="line36"></a>  message.setOptionalSint32(105);
<a name="line37"></a>  message.setOptionalFixed32(107);
<a name="line38"></a>  message.setOptionalSfixed32(109);
<a name="line39"></a>  message.setOptionalInt64(&#39;102&#39;);
<a name="line40"></a>  message.setOptionalFloat(111.5);
<a name="line41"></a>  message.setOptionalDouble(112.5);
<a name="line42"></a>  message.setOptionalBool(true);
<a name="line43"></a>  message.setOptionalString(&#39;test&#39;);
<a name="line44"></a>  message.setOptionalBytes(&#39;abcd&#39;);
<a name="line45"></a>
<a name="line46"></a>  var group = new proto2.TestAllTypes.OptionalGroup();
<a name="line47"></a>  group.setA(111);
<a name="line48"></a>
<a name="line49"></a>  message.setOptionalgroup(group);
<a name="line50"></a>
<a name="line51"></a>  var nestedMessage = new proto2.TestAllTypes.NestedMessage();
<a name="line52"></a>  nestedMessage.setB(112);
<a name="line53"></a>
<a name="line54"></a>  message.setOptionalNestedMessage(nestedMessage);
<a name="line55"></a>
<a name="line56"></a>  message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
<a name="line57"></a>
<a name="line58"></a>  // Repeated.
<a name="line59"></a>  message.addRepeatedInt32(201);
<a name="line60"></a>  message.addRepeatedInt32(202);
<a name="line61"></a>
<a name="line62"></a>  // Serialize to a simplified text format.
<a name="line63"></a>  var simplified = new goog.proto2.TextFormatSerializer().serialize(message);
<a name="line64"></a>  var expected = &#39;optional_int32: 101\n&#39; +
<a name="line65"></a>      &#39;optional_int64: 102\n&#39; +
<a name="line66"></a>      &#39;optional_uint32: 103\n&#39; +
<a name="line67"></a>      &#39;optional_sint32: 105\n&#39; +
<a name="line68"></a>      &#39;optional_fixed32: 107\n&#39; +
<a name="line69"></a>      &#39;optional_sfixed32: 109\n&#39; +
<a name="line70"></a>      &#39;optional_float: 111.5\n&#39; +
<a name="line71"></a>      &#39;optional_double: 112.5\n&#39; +
<a name="line72"></a>      &#39;optional_bool: true\n&#39; +
<a name="line73"></a>      &#39;optional_string: &quot;test&quot;\n&#39; +
<a name="line74"></a>      &#39;optional_bytes: &quot;abcd&quot;\n&#39; +
<a name="line75"></a>      &#39;optionalgroup {\n&#39; +
<a name="line76"></a>      &#39;  a: 111\n&#39; +
<a name="line77"></a>      &#39;}\n&#39; +
<a name="line78"></a>      &#39;optional_nested_message {\n&#39; +
<a name="line79"></a>      &#39;  b: 112\n&#39; +
<a name="line80"></a>      &#39;}\n&#39; +
<a name="line81"></a>      &#39;optional_nested_enum: FOO\n&#39; +
<a name="line82"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line83"></a>      &#39;repeated_int32: 202\n&#39;;
<a name="line84"></a>
<a name="line85"></a>  assertEquals(expected, simplified);
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testSerializationOfUnknown() {
<a name="line89"></a>  var nestedUnknown = new proto2.TestAllTypes();
<a name="line90"></a>  var message = new proto2.TestAllTypes();
<a name="line91"></a>
<a name="line92"></a>  // Set the fields.
<a name="line93"></a>  // Known.
<a name="line94"></a>  message.setOptionalInt32(101);
<a name="line95"></a>  message.addRepeatedInt32(201);
<a name="line96"></a>  message.addRepeatedInt32(202);
<a name="line97"></a>
<a name="line98"></a>  nestedUnknown.addRepeatedInt32(301);
<a name="line99"></a>  nestedUnknown.addRepeatedInt32(302);
<a name="line100"></a>
<a name="line101"></a>  // Unknown.
<a name="line102"></a>  message.setUnknown(1000, 301);
<a name="line103"></a>  message.setUnknown(1001, 302);
<a name="line104"></a>  message.setUnknown(1002, &#39;hello world&#39;);
<a name="line105"></a>  message.setUnknown(1002, nestedUnknown);
<a name="line106"></a>
<a name="line107"></a>  nestedUnknown.setUnknown(2000, 401);
<a name="line108"></a>
<a name="line109"></a>  // Serialize.
<a name="line110"></a>  var simplified = new goog.proto2.TextFormatSerializer().serialize(message);
<a name="line111"></a>  var expected = &#39;optional_int32: 101\n&#39; +
<a name="line112"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line113"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line114"></a>      &#39;1000: 301\n&#39; +
<a name="line115"></a>      &#39;1001: 302\n&#39; +
<a name="line116"></a>      &#39;1002 {\n&#39; +
<a name="line117"></a>      &#39;  repeated_int32: 301\n&#39; +
<a name="line118"></a>      &#39;  repeated_int32: 302\n&#39; +
<a name="line119"></a>      &#39;  2000: 401\n&#39; +
<a name="line120"></a>      &#39;}&#39;;
<a name="line121"></a>
<a name="line122"></a>  assertEquals(expected, simplified);
<a name="line123"></a>}
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Asserts that the given string value parses into the given set of tokens.
<a name="line127"></a> * @param {string} value The string value to parse.
<a name="line128"></a> * @param {Array.&lt;Object&gt; | Object} tokens The tokens to check against. If not
<a name="line129"></a> *     an array, a single token is expected.
<a name="line130"></a> * @param {boolean=} opt_ignoreWhitespace Whether whitespace tokens should be
<a name="line131"></a> *     skipped by the tokenizer.
<a name="line132"></a> */
<a name="line133"></a>function assertTokens(value, tokens, opt_ignoreWhitespace) {
<a name="line134"></a>  var tokenizer = new goog.proto2.TextFormatSerializer.Tokenizer_(
<a name="line135"></a>      value, opt_ignoreWhitespace);
<a name="line136"></a>  var tokensFound = [];
<a name="line137"></a>
<a name="line138"></a>  while (tokenizer.next()) {
<a name="line139"></a>    tokensFound.push(tokenizer.getCurrent());
<a name="line140"></a>  }
<a name="line141"></a>
<a name="line142"></a>  if (goog.typeOf(tokens) != &#39;array&#39;) {
<a name="line143"></a>    tokens = [tokens];
<a name="line144"></a>  }
<a name="line145"></a>
<a name="line146"></a>  assertEquals(tokens.length, tokensFound.length);
<a name="line147"></a>  for (var i = 0; i &lt; tokens.length; ++i) {
<a name="line148"></a>    assertToken(tokens[i], tokensFound[i]);
<a name="line149"></a>  }
<a name="line150"></a>}
<a name="line151"></a>
<a name="line152"></a>function assertToken(expected, found) {
<a name="line153"></a>  assertEquals(expected.type, found.type);
<a name="line154"></a>  if (expected.value) {
<a name="line155"></a>    assertEquals(expected.value, found.value);
<a name="line156"></a>  }
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>function testTokenizer() {
<a name="line160"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line161"></a>  assertTokens(&#39;{ 123 }&#39;, [
<a name="line162"></a>      { type: types.OPEN_BRACE },
<a name="line163"></a>      { type: types.WHITESPACE, value: &#39; &#39; },
<a name="line164"></a>      { type: types.NUMBER, value: &#39;123&#39; },
<a name="line165"></a>      { type: types.WHITESPACE, value: &#39; &#39;},
<a name="line166"></a>      { type: types.CLOSE_BRACE }
<a name="line167"></a>  ]);
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>function testTokenizerNoWhitespace() {
<a name="line171"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line172"></a>  assertTokens(&#39;{ &quot;hello world&quot; }&#39;, [
<a name="line173"></a>      { type: types.OPEN_BRACE },
<a name="line174"></a>      { type: types.STRING, value: &#39;&quot;hello world&quot;&#39; },
<a name="line175"></a>      { type: types.CLOSE_BRACE }
<a name="line176"></a>  ], true);
<a name="line177"></a>}
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>function assertIdentifier(identifier) {
<a name="line181"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line182"></a>  assertTokens(identifier, { type: types.IDENTIFIER, value: identifier });
<a name="line183"></a>}
<a name="line184"></a>
<a name="line185"></a>function assertComment(comment) {
<a name="line186"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line187"></a>  assertTokens(comment, { type: types.COMMENT, value: comment });
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>function assertString(str) {
<a name="line191"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line192"></a>  assertTokens(str, { type: types.STRING, value: str });
<a name="line193"></a>}
<a name="line194"></a>
<a name="line195"></a>function assertNumber(num) {
<a name="line196"></a>  num = num.toString();
<a name="line197"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line198"></a>  assertTokens(num, { type: types.NUMBER, value: num });
<a name="line199"></a>}
<a name="line200"></a>
<a name="line201"></a>function testTokenizerSingleTokens() {
<a name="line202"></a>  var types = goog.proto2.TextFormatSerializer.Tokenizer_.TokenTypes;
<a name="line203"></a>  assertTokens(&#39;{&#39;, { type: types.OPEN_BRACE });
<a name="line204"></a>  assertTokens(&#39;}&#39;, { type: types.CLOSE_BRACE });
<a name="line205"></a>  assertTokens(&#39;&lt;&#39;, { type: types.OPEN_TAG });
<a name="line206"></a>  assertTokens(&#39;&gt;&#39;, { type: types.CLOSE_TAG });
<a name="line207"></a>  assertTokens(&#39;:&#39;, { type: types.COLON });
<a name="line208"></a>  assertTokens(&#39;,&#39;, { type: types.COMMA });
<a name="line209"></a>  assertTokens(&#39;;&#39;, { type: types.SEMI });
<a name="line210"></a>
<a name="line211"></a>  assertIdentifier(&#39;abcd&#39;);
<a name="line212"></a>  assertIdentifier(&#39;Abcd&#39;);
<a name="line213"></a>  assertIdentifier(&#39;ABcd&#39;);
<a name="line214"></a>  assertIdentifier(&#39;ABcD&#39;);
<a name="line215"></a>  assertIdentifier(&#39;a123nc&#39;);
<a name="line216"></a>  assertIdentifier(&#39;a45_bC&#39;);
<a name="line217"></a>  assertIdentifier(&#39;A45_bC&#39;);
<a name="line218"></a>
<a name="line219"></a>  assertIdentifier(&#39;inf&#39;);
<a name="line220"></a>  assertIdentifier(&#39;infinity&#39;);
<a name="line221"></a>  assertIdentifier(&#39;nan&#39;);
<a name="line222"></a>
<a name="line223"></a>  assertNumber(0);
<a name="line224"></a>  assertNumber(10);
<a name="line225"></a>  assertNumber(123);
<a name="line226"></a>  assertNumber(1234);
<a name="line227"></a>  assertNumber(123.56);
<a name="line228"></a>  assertNumber(-124);
<a name="line229"></a>  assertNumber(-1234);
<a name="line230"></a>  assertNumber(-123.56);
<a name="line231"></a>  assertNumber(&#39;123f&#39;);
<a name="line232"></a>  assertNumber(&#39;123.6f&#39;);
<a name="line233"></a>  assertNumber(&#39;-123f&#39;);
<a name="line234"></a>  assertNumber(&#39;-123.8f&#39;);
<a name="line235"></a>  assertNumber(&#39;0x1234&#39;);
<a name="line236"></a>  assertNumber(&#39;0x12ac34&#39;);
<a name="line237"></a>  assertNumber(&#39;0x49e281db686fb&#39;);
<a name="line238"></a>
<a name="line239"></a>  assertString(&#39;&quot;&quot;&#39;);
<a name="line240"></a>  assertString(&#39;&quot;hello world&quot;&#39;);
<a name="line241"></a>  assertString(&#39;&quot;hello # world&quot;&#39;);
<a name="line242"></a>  assertString(&#39;&quot;hello #\\&quot; world&quot;&#39;);
<a name="line243"></a>  assertString(&#39;&quot;|&quot;&#39;);
<a name="line244"></a>  assertString(&#39;&quot;\\&quot;\\&quot;&quot;&#39;);
<a name="line245"></a>  assertString(&#39;&quot;\\&quot;foo\\&quot;&quot;&#39;);
<a name="line246"></a>  assertString(&#39;&quot;\\&quot;foo\\&quot; and \\&quot;bar\\&quot;&quot;&#39;);
<a name="line247"></a>  assertString(&#39;&quot;foo \\&quot;and\\&quot; bar&quot;&#39;);
<a name="line248"></a>
<a name="line249"></a>  assertComment(&#39;# foo bar baz&#39;);
<a name="line250"></a>  assertComment(&#39;# foo ## bar baz&#39;);
<a name="line251"></a>  assertComment(&#39;# foo &quot;bar&quot; baz&#39;);
<a name="line252"></a>}
<a name="line253"></a>
<a name="line254"></a>function testSerializationOfStringWithQuotes() {
<a name="line255"></a>  var nestedUnknown = new proto2.TestAllTypes();
<a name="line256"></a>  var message = new proto2.TestAllTypes();
<a name="line257"></a>  message.setOptionalString(&#39;hello &quot;world&quot;&#39;);
<a name="line258"></a>
<a name="line259"></a>  // Serialize.
<a name="line260"></a>  var simplified = new goog.proto2.TextFormatSerializer().serialize(message);
<a name="line261"></a>  var expected = &#39;optional_string: &quot;hello \\&quot;world\\&quot;&quot;\n&#39;;
<a name="line262"></a>  assertEquals(expected, simplified);
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>function testDeserialization() {
<a name="line266"></a>  var message = new proto2.TestAllTypes();
<a name="line267"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line268"></a>     &#39;repeated_int32: 201\n&#39; +
<a name="line269"></a>     &#39;repeated_int32: 202\n&#39; +
<a name="line270"></a>     &#39;optional_float: 123.4&#39;;
<a name="line271"></a>
<a name="line272"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line273"></a>
<a name="line274"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line275"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line276"></a>  assertEquals(202, message.getRepeatedInt32(1));
<a name="line277"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line278"></a>}
<a name="line279"></a>
<a name="line280"></a>function testDeserializationOfList() {
<a name="line281"></a>  var message = new proto2.TestAllTypes();
<a name="line282"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line283"></a>      &#39;repeated_int32: [201, 202]\n&#39; +
<a name="line284"></a>      &#39;optional_float: 123.4&#39;;
<a name="line285"></a>
<a name="line286"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line287"></a>
<a name="line288"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line289"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line290"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line291"></a>}
<a name="line292"></a>
<a name="line293"></a>function testDeserializationOfIntegerAsHexadecimalString() {
<a name="line294"></a>  var message = new proto2.TestAllTypes();
<a name="line295"></a>  var value = &#39;optional_int32: 0x1\n&#39; +
<a name="line296"></a>      &#39;optional_sint32: 0xf\n&#39; +
<a name="line297"></a>      &#39;optional_uint32: 0xffffffff\n&#39; +
<a name="line298"></a>      &#39;repeated_int32: [0x0, 0xff]\n&#39;;
<a name="line299"></a>
<a name="line300"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line301"></a>
<a name="line302"></a>  assertEquals(1, message.getOptionalInt32());
<a name="line303"></a>  assertEquals(15, message.getOptionalSint32());
<a name="line304"></a>  assertEquals(4294967295, message.getOptionalUint32());
<a name="line305"></a>  assertEquals(0, message.getRepeatedInt32(0));
<a name="line306"></a>  assertEquals(255, message.getRepeatedInt32(1));
<a name="line307"></a>}
<a name="line308"></a>
<a name="line309"></a>function testDeserializationOfInt64AsHexadecimalString() {
<a name="line310"></a>  var message = new proto2.TestAllTypes();
<a name="line311"></a>  var value = &#39;optional_int64: 0xf&#39;;
<a name="line312"></a>
<a name="line313"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line314"></a>
<a name="line315"></a>  assertEquals(&#39;0xf&#39;, message.getOptionalInt64());
<a name="line316"></a>}
<a name="line317"></a>
<a name="line318"></a>function testDeserializationOfZeroFalseAndEmptyString() {
<a name="line319"></a>  var message = new proto2.TestAllTypes();
<a name="line320"></a>  var value = &#39;optional_int32: 0\n&#39; +
<a name="line321"></a>      &#39;optional_bool: false\n&#39; +
<a name="line322"></a>      &#39;optional_string: &quot;&quot;&#39;;
<a name="line323"></a>
<a name="line324"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line325"></a>
<a name="line326"></a>  assertEquals(0, message.getOptionalInt32());
<a name="line327"></a>  assertEquals(false, message.getOptionalBool());
<a name="line328"></a>  assertEquals(&#39;&#39;, message.getOptionalString());
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>function testDeserializationSkipUnknown() {
<a name="line332"></a>  var message = new proto2.TestAllTypes();
<a name="line333"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line334"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line335"></a>      &#39;some_unknown: true\n&#39; +
<a name="line336"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line337"></a>      &#39;optional_float: 123.4&#39;;
<a name="line338"></a>
<a name="line339"></a>  var parser = new goog.proto2.TextFormatSerializer.Parser();
<a name="line340"></a>  assertTrue(parser.parse(message, value, true));
<a name="line341"></a>
<a name="line342"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line343"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line344"></a>  assertEquals(202, message.getRepeatedInt32(1));
<a name="line345"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line346"></a>}
<a name="line347"></a>
<a name="line348"></a>function testDeserializationSkipUnknownList() {
<a name="line349"></a>  var message = new proto2.TestAllTypes();
<a name="line350"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line351"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line352"></a>      &#39;some_unknown: [true, 1, 201, &quot;hello&quot;]\n&#39; +
<a name="line353"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line354"></a>      &#39;optional_float: 123.4&#39;;
<a name="line355"></a>
<a name="line356"></a>  var parser = new goog.proto2.TextFormatSerializer.Parser();
<a name="line357"></a>  assertTrue(parser.parse(message, value, true));
<a name="line358"></a>
<a name="line359"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line360"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line361"></a>  assertEquals(202, message.getRepeatedInt32(1));
<a name="line362"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line363"></a>}
<a name="line364"></a>
<a name="line365"></a>function testDeserializationSkipUnknownNested() {
<a name="line366"></a>  var message = new proto2.TestAllTypes();
<a name="line367"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line368"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line369"></a>      &#39;some_unknown: &lt;\n&#39; +
<a name="line370"></a>      &#39;  a: 1\n&#39; +
<a name="line371"></a>      &#39;  b: 2\n&#39; +
<a name="line372"></a>      &#39;&gt;\n&#39; +
<a name="line373"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line374"></a>      &#39;optional_float: 123.4&#39;;
<a name="line375"></a>
<a name="line376"></a>  var parser = new goog.proto2.TextFormatSerializer.Parser();
<a name="line377"></a>  assertTrue(parser.parse(message, value, true));
<a name="line378"></a>
<a name="line379"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line380"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line381"></a>  assertEquals(202, message.getRepeatedInt32(1));
<a name="line382"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line383"></a>}
<a name="line384"></a>
<a name="line385"></a>function testDeserializationSkipUnknownNestedInvalid() {
<a name="line386"></a>  var message = new proto2.TestAllTypes();
<a name="line387"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line388"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line389"></a>      &#39;some_unknown: &lt;\n&#39; +
<a name="line390"></a>      &#39;  a: \n&#39; + // Missing value.
<a name="line391"></a>      &#39;  b: 2\n&#39; +
<a name="line392"></a>      &#39;&gt;\n&#39; +
<a name="line393"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line394"></a>      &#39;optional_float: 123.4&#39;;
<a name="line395"></a>
<a name="line396"></a>  var parser = new goog.proto2.TextFormatSerializer.Parser();
<a name="line397"></a>  assertFalse(parser.parse(message, value, true));
<a name="line398"></a>}
<a name="line399"></a>
<a name="line400"></a>function testDeserializationSkipUnknownNestedInvalid2() {
<a name="line401"></a>  var message = new proto2.TestAllTypes();
<a name="line402"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line403"></a>      &#39;repeated_int32: 201\n&#39; +
<a name="line404"></a>      &#39;some_unknown: &lt;\n&#39; +
<a name="line405"></a>      &#39;  a: 2\n&#39; +
<a name="line406"></a>      &#39;  b: 2\n&#39; +
<a name="line407"></a>      &#39;}\n&#39; + // Delimiter mismatch
<a name="line408"></a>      &#39;repeated_int32: 202\n&#39; +
<a name="line409"></a>      &#39;optional_float: 123.4&#39;;
<a name="line410"></a>
<a name="line411"></a>  var parser = new goog.proto2.TextFormatSerializer.Parser();
<a name="line412"></a>  assertFalse(parser.parse(message, value, true));
<a name="line413"></a>}
<a name="line414"></a>
<a name="line415"></a>
<a name="line416"></a>function testDeserializationLegacyFormat() {
<a name="line417"></a>  var message = new proto2.TestAllTypes();
<a name="line418"></a>  var value = &#39;optional_int32: 101,\n&#39; +
<a name="line419"></a>      &#39;repeated_int32: 201,\n&#39; +
<a name="line420"></a>      &#39;repeated_int32: 202;\n&#39; +
<a name="line421"></a>      &#39;optional_float: 123.4&#39;;
<a name="line422"></a>
<a name="line423"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line424"></a>
<a name="line425"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line426"></a>  assertEquals(201, message.getRepeatedInt32(0));
<a name="line427"></a>  assertEquals(202, message.getRepeatedInt32(1));
<a name="line428"></a>  assertEquals(123.4, message.getOptionalFloat());
<a name="line429"></a>}
<a name="line430"></a>
<a name="line431"></a>function testDeserializationVariedNumbers() {
<a name="line432"></a>  var message = new proto2.TestAllTypes();
<a name="line433"></a>  var value = (
<a name="line434"></a>      &#39;repeated_int32: 23\n&#39; +
<a name="line435"></a>      &#39;repeated_int32: -3\n&#39; +
<a name="line436"></a>      &#39;repeated_int32: 0xdeadbeef\n&#39; +
<a name="line437"></a>      &#39;repeated_float: 123.0\n&#39; +
<a name="line438"></a>      &#39;repeated_float: -3.27\n&#39; +
<a name="line439"></a>      &#39;repeated_float: -35.5f\n&#39;
<a name="line440"></a>      );
<a name="line441"></a>
<a name="line442"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line443"></a>
<a name="line444"></a>  assertEquals(23, message.getRepeatedInt32(0));
<a name="line445"></a>  assertEquals(-3, message.getRepeatedInt32(1));
<a name="line446"></a>  assertEquals(3735928559, message.getRepeatedInt32(2));
<a name="line447"></a>  assertEquals(123.0, message.getRepeatedFloat(0));
<a name="line448"></a>  assertEquals(-3.27, message.getRepeatedFloat(1));
<a name="line449"></a>  assertEquals(-35.5, message.getRepeatedFloat(2));
<a name="line450"></a>}
<a name="line451"></a>
<a name="line452"></a>function testParseNumericalConstant() {
<a name="line453"></a>  var parseNumericalConstant =
<a name="line454"></a>      goog.proto2.TextFormatSerializer.Parser.parseNumericalConstant_;
<a name="line455"></a>
<a name="line456"></a>  assertEquals(Infinity, parseNumericalConstant(&#39;inf&#39;));
<a name="line457"></a>  assertEquals(Infinity, parseNumericalConstant(&#39;inff&#39;));
<a name="line458"></a>  assertEquals(Infinity, parseNumericalConstant(&#39;infinity&#39;));
<a name="line459"></a>  assertEquals(Infinity, parseNumericalConstant(&#39;infinityf&#39;));
<a name="line460"></a>  assertEquals(Infinity, parseNumericalConstant(&#39;Infinityf&#39;));
<a name="line461"></a>
<a name="line462"></a>  assertEquals(-Infinity, parseNumericalConstant(&#39;-inf&#39;));
<a name="line463"></a>  assertEquals(-Infinity, parseNumericalConstant(&#39;-inff&#39;));
<a name="line464"></a>  assertEquals(-Infinity, parseNumericalConstant(&#39;-infinity&#39;));
<a name="line465"></a>  assertEquals(-Infinity, parseNumericalConstant(&#39;-infinityf&#39;));
<a name="line466"></a>  assertEquals(-Infinity, parseNumericalConstant(&#39;-Infinity&#39;));
<a name="line467"></a>
<a name="line468"></a>  assertNull(parseNumericalConstant(&#39;-infin&#39;));
<a name="line469"></a>  assertNull(parseNumericalConstant(&#39;infin&#39;));
<a name="line470"></a>  assertNull(parseNumericalConstant(&#39;-infinite&#39;));
<a name="line471"></a>
<a name="line472"></a>  assertNull(parseNumericalConstant(&#39;-infin&#39;));
<a name="line473"></a>  assertNull(parseNumericalConstant(&#39;infin&#39;));
<a name="line474"></a>  assertNull(parseNumericalConstant(&#39;-infinite&#39;));
<a name="line475"></a>
<a name="line476"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;Nan&#39;)));
<a name="line477"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;NaN&#39;)));
<a name="line478"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;NAN&#39;)));
<a name="line479"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;nan&#39;)));
<a name="line480"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;nanf&#39;)));
<a name="line481"></a>  assertTrue(isNaN(parseNumericalConstant(&#39;NaNf&#39;)));
<a name="line482"></a>
<a name="line483"></a>  assertEquals(Number.POSITIVE_INFINITY, parseNumericalConstant(&#39;infinity&#39;));
<a name="line484"></a>  assertEquals(Number.NEGATIVE_INFINITY, parseNumericalConstant(&#39;-inf&#39;));
<a name="line485"></a>  assertEquals(Number.NEGATIVE_INFINITY, parseNumericalConstant(&#39;-infinity&#39;));
<a name="line486"></a>
<a name="line487"></a>  assertNull(parseNumericalConstant(&#39;na&#39;));
<a name="line488"></a>  assertNull(parseNumericalConstant(&#39;-nan&#39;));
<a name="line489"></a>  assertNull(parseNumericalConstant(&#39;none&#39;));
<a name="line490"></a>}
<a name="line491"></a>
<a name="line492"></a>function testDeserializationOfNumericalConstants() {
<a name="line493"></a>
<a name="line494"></a>  var message = new proto2.TestAllTypes();
<a name="line495"></a>  var value = (
<a name="line496"></a>      &#39;repeated_float: inf\n&#39; +
<a name="line497"></a>      &#39;repeated_float: -inf\n&#39; +
<a name="line498"></a>      &#39;repeated_float: nan\n&#39; +
<a name="line499"></a>      &#39;repeated_float: 300.2\n&#39;
<a name="line500"></a>      );
<a name="line501"></a>
<a name="line502"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line503"></a>
<a name="line504"></a>  assertEquals(Infinity, message.getRepeatedFloat(0));
<a name="line505"></a>  assertEquals(-Infinity, message.getRepeatedFloat(1));
<a name="line506"></a>  assertTrue(isNaN(message.getRepeatedFloat(2)));
<a name="line507"></a>  assertEquals(300.2, message.getRepeatedFloat(3));
<a name="line508"></a>}
<a name="line509"></a>
<a name="line510"></a>function testGetNumberFromString() {
<a name="line511"></a>  var getNumberFromString =
<a name="line512"></a>      goog.proto2.TextFormatSerializer.Parser.getNumberFromString_;
<a name="line513"></a>
<a name="line514"></a>  assertEquals(3735928559, getNumberFromString(&#39;0xdeadbeef&#39;));
<a name="line515"></a>  assertEquals(4276215469, getNumberFromString(&#39;0xFEE1DEAD&#39;));
<a name="line516"></a>  assertEquals(123.1, getNumberFromString(&#39;123.1&#39;));
<a name="line517"></a>  assertEquals(123.0, getNumberFromString(&#39;123.0&#39;));
<a name="line518"></a>  assertEquals(-29.3, getNumberFromString(&#39;-29.3f&#39;));
<a name="line519"></a>  assertEquals(23, getNumberFromString(&#39;23&#39;));
<a name="line520"></a>  assertEquals(-3, getNumberFromString(&#39;-3&#39;));
<a name="line521"></a>  assertEquals(-3.27, getNumberFromString(&#39;-3.27&#39;));
<a name="line522"></a>
<a name="line523"></a>  assertThrows(goog.partial(getNumberFromString, &#39;cat&#39;));
<a name="line524"></a>  assertThrows(goog.partial(getNumberFromString, &#39;NaN&#39;));
<a name="line525"></a>  assertThrows(goog.partial(getNumberFromString, &#39;inf&#39;));
<a name="line526"></a>}
<a name="line527"></a>
<a name="line528"></a>function testDeserializationError() {
<a name="line529"></a>  var message = new proto2.TestAllTypes();
<a name="line530"></a>  var value = &#39;optional_int33: 101\n&#39;;
<a name="line531"></a>  var result =
<a name="line532"></a>      new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line533"></a>  assertEquals(result, &#39;Unknown field: optional_int33&#39;);
<a name="line534"></a>}
<a name="line535"></a>
<a name="line536"></a>function testNestedDeserialization() {
<a name="line537"></a>  var message = new proto2.TestAllTypes();
<a name="line538"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line539"></a>      &#39;optional_nested_message: {\n&#39; +
<a name="line540"></a>      &#39;  b: 301\n&#39; +
<a name="line541"></a>      &#39;}&#39;;
<a name="line542"></a>
<a name="line543"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line544"></a>
<a name="line545"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line546"></a>  assertEquals(301, message.getOptionalNestedMessage().getB());
<a name="line547"></a>}
<a name="line548"></a>
<a name="line549"></a>function testNestedDeserializationLegacyFormat() {
<a name="line550"></a>  var message = new proto2.TestAllTypes();
<a name="line551"></a>  var value = &#39;optional_int32: 101\n&#39; +
<a name="line552"></a>      &#39;optional_nested_message: &lt;\n&#39; +
<a name="line553"></a>      &#39;  b: 301\n&#39; +
<a name="line554"></a>      &#39;&gt;&#39;;
<a name="line555"></a>
<a name="line556"></a>  new goog.proto2.TextFormatSerializer().deserializeTo(message, value);
<a name="line557"></a>
<a name="line558"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line559"></a>  assertEquals(301, message.getOptionalNestedMessage().getB());
<a name="line560"></a>}
<a name="line561"></a>
<a name="line562"></a>function testBidirectional() {
<a name="line563"></a>  var message = new proto2.TestAllTypes();
<a name="line564"></a>
<a name="line565"></a>  // Set the fields.
<a name="line566"></a>  // Singular.
<a name="line567"></a>  message.setOptionalInt32(101);
<a name="line568"></a>  message.setOptionalInt64(&#39;102&#39;);
<a name="line569"></a>  message.setOptionalUint32(103);
<a name="line570"></a>  message.setOptionalUint64(&#39;104&#39;);
<a name="line571"></a>  message.setOptionalSint32(105);
<a name="line572"></a>  message.setOptionalSint64(&#39;106&#39;);
<a name="line573"></a>  message.setOptionalFixed32(107);
<a name="line574"></a>  message.setOptionalFixed64(&#39;108&#39;);
<a name="line575"></a>  message.setOptionalSfixed32(109);
<a name="line576"></a>  message.setOptionalSfixed64(&#39;110&#39;);
<a name="line577"></a>  message.setOptionalFloat(111.5);
<a name="line578"></a>  message.setOptionalDouble(112.5);
<a name="line579"></a>  message.setOptionalBool(true);
<a name="line580"></a>  message.setOptionalString(&#39;test&#39;);
<a name="line581"></a>  message.setOptionalBytes(&#39;abcd&#39;);
<a name="line582"></a>
<a name="line583"></a>  var group = new proto2.TestAllTypes.OptionalGroup();
<a name="line584"></a>  group.setA(111);
<a name="line585"></a>
<a name="line586"></a>  message.setOptionalgroup(group);
<a name="line587"></a>
<a name="line588"></a>  var nestedMessage = new proto2.TestAllTypes.NestedMessage();
<a name="line589"></a>  nestedMessage.setB(112);
<a name="line590"></a>
<a name="line591"></a>  message.setOptionalNestedMessage(nestedMessage);
<a name="line592"></a>
<a name="line593"></a>  message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
<a name="line594"></a>
<a name="line595"></a>  // Repeated.
<a name="line596"></a>  message.addRepeatedInt32(201);
<a name="line597"></a>  message.addRepeatedInt32(202);
<a name="line598"></a>  message.addRepeatedString(&#39;hello &quot;world&quot;&#39;);
<a name="line599"></a>
<a name="line600"></a>  // Serialize the message to text form.
<a name="line601"></a>  var serializer = new goog.proto2.TextFormatSerializer();
<a name="line602"></a>  var textform = serializer.serialize(message);
<a name="line603"></a>
<a name="line604"></a>  // Create a copy and deserialize into the copy.
<a name="line605"></a>  var copy = new proto2.TestAllTypes();
<a name="line606"></a>  serializer.deserializeTo(copy, textform);
<a name="line607"></a>
<a name="line608"></a>  // Assert that the messages are structurally equivalent.
<a name="line609"></a>  assertTrue(copy.equals(message));
<a name="line610"></a>}
<a name="line611"></a>
<a name="line612"></a>
<a name="line613"></a>function testBidirectional64BitNumber() {
<a name="line614"></a>  var message = new proto2.TestAllTypes();
<a name="line615"></a>  message.setOptionalInt64Number(10000000);
<a name="line616"></a>  message.setOptionalInt64String(&#39;200000000000000000&#39;);
<a name="line617"></a>
<a name="line618"></a>  // Serialize the message to text form.
<a name="line619"></a>  var serializer = new goog.proto2.TextFormatSerializer();
<a name="line620"></a>  var textform = serializer.serialize(message);
<a name="line621"></a>
<a name="line622"></a>  // Create a copy and deserialize into the copy.
<a name="line623"></a>  var copy = new proto2.TestAllTypes();
<a name="line624"></a>  serializer.deserializeTo(copy, textform);
<a name="line625"></a>
<a name="line626"></a>  // Assert that the messages are structurally equivalent.
<a name="line627"></a>  assertTrue(copy.equals(message));
<a name="line628"></a>}
</pre>


</body>
</html>
